// pages/My/ bank-card.js
const Controllers = require('../controllers/ControllerMy.js').controller;
const getCode = require('../controllers/Controller.js').controller.getCode;
const utils = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: false,
    columns: [
      {
        laber: '持卡人',
        key: 'bankNick',
        placeholder: '请输入持卡人真实姓名'
      },
      {
        laber: '银行卡号',
        key: 'bankAccount',
        placeholder: '持卡人本人银行卡号'
      },
      {
        laber: '开户银行',
        key: 'bankName',
        placeholder: '请输入银行卡开户行'
      },
      {
        laber: '银行支行',
        key: 'bankAddress',
        placeholder: '请输入银行卡开户支行'
      },
      {
        laber: '手机号',
        key: 'mobile',
        placeholder: '请输入银行预留手机号'
      },
      {
        laber: '验证码',
        key: 'code',
        placeholder: '请输入校验码'
      }
    ],
    codeBtnText: '获取验证码',
    codeDisabled: true,
    isAgree: false,
    bankData: {}
    // bankData: {
    //   bankNick: '测试',
    //   bankName: '中国银行',
    //   bankAddress: '中国银行测试支行',
    //   bankAccount: '13123123123123123',
    //   mobile: '13800000000',
    //   code: '123',
    //   state: 0
    // }
  },
  changeBank() {
    var that = this;
    var bankData = that.data.bankData;
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    wx.showLoading();
    if (bankData && bankData.id) {
      // 修改银行卡；
      Controllers.updateAgentBank(bankData).then(res => {
        console.log('updateAgentBank--res==', res);
        const { code, data, desc } = res;
        wx.showToast({
          title: desc,
          none: code === 1 ? 'success' : 'none'
        })
        if (that.codeInterva) {
          clearInterval(that.codeInterva);
        }
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
        that.setData({
          bankData: data,
          codeDisabled: false,
          codeBtnText: '获取验证码'
        })
      });
      wx.hideLoading();
      that.data.isLoading = false;
    } else {
      Controllers.saveAgentBank(bankData).then(res => {
        console.log('saveAgentBank--res==', res);
        const { code, data, desc } = res;
        wx.showToast({
          title: desc,
          none: code === 1 ? 'success' : 'none'
        })
        if (that.codeInterva) {
          clearInterval(that.codeInterva);
        }
        that.setData({
          bankData: data,
          codeDisabled: false,
          codeBtnText: '获取验证码'
        })
      });
      wx.hideLoading();
      that.data.isLoading = false;
    }
  },
  getAgentBank() {
    var that = this;
    wx.showLoading();
    Controllers.getAgentBank({}).then(res => {
      console.log('getAgentBank--res==', res);
      const bankData = res.data || that.data.bankData;
      var codeDisabled = true;
      if (bankData && bankData.mobile && utils.checkPhone(bankData.mobile)) {
        codeDisabled = false;
      }
      that.setData({
        bankData,
        codeDisabled
      });
      wx.hideLoading();
    })
  },
  changeAgree() {
    var that = this;
    that.setData({
      isAgree: !that.data.isAgree
    });
  },
  getCode(e) {
    var that = this;
    var codeDisabled = that.data.codeDisabled;
    if (codeDisabled) {
      return;
    }
    var codeIntervalNumber = 60;
    var codeBtnText = '';
    that.codeInterva = setInterval(() => {
      if (codeIntervalNumber > 1) {
        codeDisabled = true;
        codeIntervalNumber -= 1;
        codeBtnText = `${codeIntervalNumber}s`;
      } else {
        codeDisabled = false;
        codeBtnText = '获取验证码';
        clearInterval(that.codeInterva);
      }
      that.setData({
        codeDisabled,
        codeBtnText
      })
    }, 1000);
    var params={
      mobile: that.data.bankData.mobile,
      type: 'Bank',
      sendType: 3
    }

    getCode(params).then(res=>{
      console.log('getCode--res==', res);
      wx.showToast({
        title: '发送成功',
      });
    })
  },
  getInput(e) {
    var that = this;
    var name = e.target.dataset.name;
    var value = e.detail.value;
    var bankData = that.data.bankData;
    if (name === 'mobile') {
      if (value.length === 11) {
        if (!utils.checkPhone(value)) {
          wx.showToast({
            title: '手机号码格式不正确',
            icon: 'none'
          });
          return;
        } else {
          bankData[name] = value;
          var codeDisabled = true;
          if (!that.codeInterva) {
            codeDisabled = false;
          }
          console.log('that.codeInterva==', that.codeInterva);
          console.log('codeDisabled==', codeDisabled);
          that.setData({
            bankData,
            codeDisabled
          });
        }
      }
      return;
    }
    bankData[name] = value;

    that.setData({
      bankData
    })
    console.log('that.data.bankData==', that.data.bankData);
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getAgentBank();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})